package com.siteseat.crudeq.common.raw.protocol.v1;

import java.util.HashMap;
import java.util.Map;

import com.siteseat.common.exception.InternalException;

/**
 *
 */
public enum ErrorCode {

  AUTHENTICATION_FAILED( 1 ),

  INVALID_REQUEST( 2 ),

  NOT_AUTHENTICATED_YET( 3 ),

  INVALID_REQUEST_VERSION( 4 ),

  INTERNAL_ERROR( 5 ),

  SECURITY_ERROR( 6 ),
  
  SERVER_NOT_ENABLED_YET( 7 );

  private Integer id;

  private static Map<Integer, ErrorCode> map = new HashMap<Integer, ErrorCode>();
  static {
    register( AUTHENTICATION_FAILED );
    register( INVALID_REQUEST );
    register( NOT_AUTHENTICATED_YET );
    register( INVALID_REQUEST_VERSION );
    register( INTERNAL_ERROR );
    register( SECURITY_ERROR );
    register( SERVER_NOT_ENABLED_YET );
  }

  /**
   * @param c
   */
  private static void register( ErrorCode c ) {
    if( map.containsKey( c.id ) ) {
      throw new InternalException( "Duplicated error code: " + c + "(" + c.id + ")" );
    }
    map.put( c.id, c );
  }

  /**
   * @param id
   */
  private ErrorCode( int id ) {
    this.id = Integer.valueOf( id );
  }

  /**
   * @return
   */
  public int id() {
    return this.id.intValue();
  }

  /**
   * @param id
   * @return
   */
  public static ErrorCode get( int id ) {
    return map.get( Integer.valueOf( id ) );
  }

  /**
   * @param id
   * @return
   */
  public static ErrorCode get( Integer id ) {
    return get( id.intValue() );
  }

}
